package com.usian.controller;

import com.usian.feign.UserFeign;
import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserFeign userFeign;

    @RequestMapping("consumer/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userFeign.getUserById(id);
    }

    @RequestMapping("consumer/findAll")
    public List<User> findAll() {
        /*ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        String url = "http://"+si.getHost()+":"+si.getPort()+"/findAll/";
        return restTemplate.getForObject(url, List.class);*/
        return userFeign.findAll();
    }

    @RequestMapping("/consumer/findByName")
    public User findByName(@RequestParam String name){
        return userFeign.findByName(name);
    }

    @RequestMapping("/consumer/findUser")
    public User findUser(){
        User user = new User();
        user.setId(10);
        user.setName("张张");
        user.setAge(28);
        user.setAddress("南京");
        user.setBirthday(new Date());
        return userFeign.findUser(user);
    }
}
